﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;

using DevTeamIT;
using Extensions;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Araclar
{
    public partial class ExceptionViewer : XForm
    {
        public ExceptionViewer()
        {
            InitializeComponent();
        }

        private void ExceptionViewer_Load(object sender, EventArgs e)
        {

            gv.Init("ConfExpert.dbo.GetBildirilenException");

            RepositoryItemMemoExEdit mex = new DevExpress.XtraEditors.Repository.RepositoryItemMemoExEdit();
            mex.ShowIcon = false;
            mex.ScrollBars = ScrollBars.Vertical;
            mex.PopupFormMinSize = new System.Drawing.Size(750, 400);

            gv.Columns["Notlar"].ColumnEdit =
            gv.Columns["ErrorMsg"].ColumnEdit = mex;
            gv.Columns["AnsweredBy"].ColumnEdit = RI(Utility.Rp.V_User);
            gv.Columns["AnsweredOn"].DisplayFormat.FormatString =
            gv.Columns["Tarih"].DisplayFormat.FormatString = "dd.MM.yyyy HH:mm";

            gv.MakeColumnInvisible("Screenshot", "Form", "App", "isResolved");
            gv.YanYanaGetirCoklu("AnsweredBy", "ErrorMsg", "Notlar");
            gv.AddSummary();

            gv.BestFitColumns();
            gv.Columns["Notlar"].Width = 360;
            gv.Columns["ErrorMsg"].Width = 600;

            Utility.Grid.GridManager gm = Utility.Grid.GridManager.GetManager(gv);
            gm.GridMenu.AddMenu("Yanıtla", Yanitla);
        }

        private void Yanitla(object sender, EventArgs e)
        {
            DataRow dr = gv.GetFocusedDataRow();
            ITaskManager.Datasets.MyData.V_UserRow user = ConfExpert.Utility.RepositoryManager.dsi.V_User.FindByUserId(dr["UserId"].ToInt(-1));
            string notlar = dr["Notlar"].ToStr().Split("|".ToCharArray())[0].Replace("Mesaj:", "");
            string name = user.Name;

            string icerik = "<span style='font-family:verdana;font-size:8'>" + name + ", <p><p><p>" + "&nbsp;" +
                             "<p>-------------------------------------------------------------------" +
                             "<br><b>Tarih:&nbsp;</b>" + ((DateTime)dr["Tarih"]).ToString("dd.MM.yyyy HH:mm") +
                             "<br><b>Mesaj:&nbsp;</b>" + notlar + "</span>";

            EmailSender ey = new EmailSender(user.Email, "", "RE: ConfExpert - Hata Kodu:" + dr["ExceptionId"].ToStr(), icerik, name.Length + 3);
            ey.Text = "Exception Yanıtla";

            if (ey.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                Session.DataManager.ExecuteNonQuery("ConfExpert.dbo.ExceptionYanitla", dr["ExceptionId"].ToInt(-1), WhoAmI);

            btnGuncelle.PerformClick();
        }

        private void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.VeriyiGuncelle("ConfExpert.dbo.GetBildirilenException");
            gv.BestFitColumns();
            gv.Columns["Notlar"].Width = 50;
        }
    }
}
